﻿using System;
using System.Linq;
using System.Windows.Forms;
using Jage.Win32;

namespace Jage.GameEditor.Controls.Spritesheets
{
    public partial class SpritesheetsEditor : UserControl
    {

        public SpritesheetsEditor()
        {
            InitializeComponent();
            DefineSpritesheetsDatabinder();
        }

        #region Methods

        public void Initialize()
        {
            RefreshSpritesheets();
        }

        private void RefreshPreview()
        {
            if (_spritesheetsDatabinder.SelectedObjects.Count() > 0)
            {
                panel2.Enabled = true;
                spritesheetPreview1.SetSpritesheet(_spritesheetsDatabinder.SelectedObjects.First());
            }
            else
            {
                panel2.Enabled = false;
                spritesheetPreview1.SetSpritesheet(null);
            }
        }

        private void EditSpritesheet(Data.Spritesheet spritesheet)
        {
            ClearPreview();
            var editor = new SpritesheetEditor { Spritesheet = spritesheet };
            if (editor.ShowDialog() == DialogResult.OK)
            {
                RefreshSpritesheets();
                RefreshPreview();
            }
        }

        private void ClearPreview()
        {
            spritesheetPreview1.SetSpritesheet(null);
        }


        private ListViewDatabinder<Data.Spritesheet> _spritesheetsDatabinder;

        private void DefineSpritesheetsDatabinder()
        {
            _spritesheetsDatabinder = new ListViewDatabinder<Data.Spritesheet>(listViewSpritesheets, "SpritesheetId");
            _spritesheetsDatabinder.AddColumnDefinition("Name");
        }

        private void RefreshSpritesheets()
        {
            _spritesheetsDatabinder.Databind(from spritesheet in Data.JageDb.Default.Spritesheets orderby spritesheet.Name select spritesheet);
        }

        #endregion

        #region EventHandlers

        #region Spritesheets

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string spritesheetName;
            if (InputBox.ShowDialog("Add a spritesheet", "Enter a name for the new spritesheet:", String.Empty, out spritesheetName, s => (from ss in Data.JageDb.Default.Spritesheets where ss.Name.ToLower() == s.ToLower() select ss).Count() == 0, "A spritesheet name must be unique.") == DialogResult.OK)
            {
                var context = Data.JageDb.Default;
                context.Spritesheets.AddObject(Logic.Crud.Spritesheet.GetNew(spritesheetName, 10));
                context.SaveChanges();
                RefreshSpritesheets();
            }
        }

        private void ListViewSpritesheetsSelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshPreview();
        }

        private void ToolStripButton2Click(object sender, EventArgs e)
        {
            if (listViewSpritesheets.SelectedItems.Count > 0)
            {
                if (MessageBox.Show(string.Format("Delete {0} spritesheets and its/their content?", listViewSpritesheets.SelectedItems.Count), @"Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BusyBox.ShowBusy("Deleting...");
                    spritesheetPreview1.SetSpritesheet(null); // remove possible handle on file
                    try
                    {
                        foreach (var spritesheet in _spritesheetsDatabinder.SelectedObjects)
                        {
                            Logic.Crud.Spritesheet.Delete(spritesheet);
                        }
                        RefreshSpritesheets();
                        RefreshPreview();
                    }
                    finally
                    {
                        BusyBox.HideBusy();
                    }
                }
            }
        }

        private void listViewSpritesheets_DoubleClick(object sender, EventArgs e)
        {
            if (_spritesheetsDatabinder.SelectedObjects.Count() > 0)
            {
                EditSpritesheet(_spritesheetsDatabinder.SelectedObjects.First());
                RefreshSpritesheets();
            }
        }

        #endregion

        #region Sprites
        
        #endregion

        #endregion



    }
}
